//
//  CQueue.swift
//  LeetCodeSummary
//
//  Created by WangYonghe on 2020/6/30.
//  Copyright © 2020 WangYonghe. All rights reserved.
//  剑指 Offer 09. 用两个栈实现队列

import UIKit

/*
 剑指 Offer 09. 用两个栈实现队列
 用两个栈实现一个队列。队列的声明如下，请实现它的两个函数 appendTail 和 deleteHead ，分别完成在队列尾部插入整数和在队列头部删除整数的功能。(若队列中没有元素，deleteHead 操作返回 -1 )

  

 示例 1：

 输入：
 ["CQueue","appendTail","deleteHead","deleteHead"]
 [[],[3],[],[]]
 输出：[null,null,3,-1]
 示例 2：

 输入：
 ["CQueue","deleteHead","appendTail","appendTail","deleteHead","deleteHead"]
 [[],[],[5],[2],[],[]]
 输出：[null,-1,null,null,5,2]
 提示：

 1 <= values <= 10000
 最多会对 appendTail、deleteHead 进行 10000 次调用
 */

class CQueue: NSObject {

    var stack1:MyStack = MyStack()
    var stack2:MyStack = MyStack()
    
    func appendTail(_ value: Int) {
        stack1.push(value)
    }
    
    func deleteHead() -> Int {
        if stack2.isEmpty == true{
            while stack1.isEmpty == false {
                stack2.push(stack1.pop())
            }
        }
        if stack2.isEmpty == true {
            return -1
        }else{
            let deleItem = stack2.pop()
            return deleItem
        }
    }
}

public class MyStack: NSObject{
    public var stackSource: Array<Int> = []
    public var size:Int{
        return stackSource.count
    }
    public var isEmpty: Bool{
        return stackSource.count == 0
    }
    
    public func push(_ elememt: Int){
        stackSource.append(elememt)
    }
    
    public func pop() -> Int{
        if let last = stackSource.last{
            stackSource.removeLast()
            return last
            
        }else{
            return -1
        }
    }
    
}
